package com.yy.finvideoplayer.utils

import android.graphics.Color
import android.text.TextUtils
import androidx.annotation.ColorInt

/**
 * @Description: ColorUtil
 * @Author: yiw89
 * @CreateDate: 2025/9/15 11:57
 * @Version: 1.0
 */
object ColorUtil {
    /**
     * 解析颜色值，支持"#rrggbb"和"#rgb"格式
     *
     * @param colorString  颜色字符串
     * @param defaultColor 默认颜色值，解析失败时返回
     * @return 颜色值
     */
    /**
     * 解析颜色值，支持"#rrggbb"和"#rgb"格式
     *
     * @param colorString 颜色字符串
     * @return 颜色值
     */
    @JvmOverloads
    fun parseColor(colorString: String, defaultColor: Int = Color.TRANSPARENT): Int {
        if (TextUtils.isEmpty(colorString)) {
            return defaultColor
        }

        if (colorString[0] == '#' && colorString.length == 4) {
            val r = colorString[1]
            val g = colorString[2]
            val b = colorString[3]
            val rrggbb = StringBuilder()
                .append("#")
                .append(r).append(r)
                .append(g).append(g)
                .append(b).append(b)
                .toString()
            return Color.parseColor(rrggbb)
        } else if (colorString[0] == '#' && colorString.length == 5) {
            val a = colorString[1]
            val r = colorString[2]
            val g = colorString[3]
            val b = colorString[4]
            val rrggbb = "#" +
                    a + a +
                    r + r +
                    g + g +
                    b + b
            return Color.parseColor(rrggbb)
        }

        return try {
            Color.parseColor(colorString)
        } catch (e: Exception) {
            defaultColor
        }
    }

    /**
     * 解析颜色值，支持"#rrggbb","#rrggbbaa"和"#rgb"格式
     *
     * @param colorString  颜色字符串
     * @param defaultColor 默认颜色值，解析失败时返回
     * @return 颜色值
     */
    /**
     * 解析颜色值，支持"#rrggbb","#rrggbbaa"和"#rgb"格式
     *
     * @param colorString 颜色字符串
     * @return 颜色值
     */
    @JvmOverloads
    fun parseRGBA(colorString: String, defaultColor: Int = Color.TRANSPARENT): Int {
        if (TextUtils.isEmpty(colorString)) {
            return defaultColor
        }
        if (colorString[0] == '#' && colorString.length == 4) {
            val r = colorString[1]
            val g = colorString[2]
            val b = colorString[3]
            val rrggbb = "#" +
                    r + r +
                    g + g +
                    b + b
            return Color.parseColor(rrggbb)
        } else if (colorString[0] == '#' && colorString.length == 5) {
            val r = colorString[1]
            val g = colorString[2]
            val b = colorString[3]
            val a = colorString[4]
            val rrggbb = "#" +
                    a + a +
                    r + r +
                    g + g +
                    b + b
            return Color.parseColor(rrggbb)
        }
        if (colorString[0] == '#' && colorString.length == 9) {
            val rgba =
                colorString[0].toString() + colorString.substring(7, 9) + colorString.substring(
                    1,
                    7
                )
            return Color.parseColor(rgba)
        }

        return try {
            Color.parseColor(colorString)
        } catch (e: Exception) {
            defaultColor
        }
    }

    /**
     * 解析颜色值，支持"#rrggbb","#rgb"格式
     *
     * @param colorString 颜色字符串
     * @return 颜色值
     */
    fun parseRGBSafely(colorString: String): Int {
        return parseRGBSafely(colorString, Color.TRANSPARENT)
    }

    /**
     * 解析颜色值，支持"#rrggbb","#rgb"格式
     *
     * @param colorString  颜色字符串
     * @param defaultColor 默认颜色值，解析失败时返回
     * @return 颜色值
     */
    private fun parseRGBSafely(colorString: String, defaultColor: Int): Int {
        try {
            return parseRGB(colorString, defaultColor)
        } catch (e: Exception) {
            e.printStackTrace()
            return defaultColor
        }
    }

    /**
     * 解析颜色值，支持"#rrggbb","#rgb"格式
     *
     * @param colorString  颜色字符串
     * @param defaultColor 默认颜色值，解析失败时返回
     * @return 颜色值
     */
    private fun parseRGB(colorString: String, defaultColor: Int): Int {
        if (TextUtils.isEmpty(colorString)) {
            return defaultColor
        }

        // Check if string starts with #
        if (!colorString.startsWith("#")) {
            return defaultColor
        }

        // Remove # prefix for validation
        val hexColor = colorString.substring(1)

        // Validate hex characters
        if (!hexColor.matches("[0-9A-Fa-f]+".toRegex())) {
            return defaultColor
        }

        // Handle #RGB or #RGBA format
        if (hexColor.length == 3 || hexColor.length == 4) {
            val expanded = StringBuilder("#")
            for (i in 0..2) {
                expanded.append(hexColor[i]).append(hexColor[i])
            }
            return Color.parseColor(expanded.toString())
        }

        // Handle #RRGGBB format
        if (hexColor.length == 6) {
            return Color.parseColor(colorString)
        }

        // Handle #RRGGBBAA format
        if (hexColor.length == 8) {
            return Color.parseColor("#" + hexColor.substring(0, 6))
        }

        return defaultColor
    }

    /**
     * 根据颜色和透明度计算颜色
     *
     * @param color color值
     * @param alpha alpha值
     * @return 最终的颜色
     */
    fun calculateColor(@ColorInt color: Int, alpha: Int): Int {
        if (alpha == 0) {
            return color
        }
        val a = 1 - alpha / 255f
        var red = color shr 16 and 0xff
        var green = color shr 8 and 0xff
        var blue = color and 0xff
        red = (red * a + 0.5).toInt()
        green = (green * a + 0.5).toInt()
        blue = (blue * a + 0.5).toInt()
        return 0xff shl 24 or (red shl 16) or (green shl 8) or blue
    }

    fun composeColor(color: Int, another: Int): Int {
        val a = Color.alpha(color) / 255f
        val red =
            (Color.red(color) * a + 0.5).toInt() + (Color.red(another) * (1 - a) + 0.5).toInt()
        val green =
            (Color.green(color) * a + 0.5).toInt() + (Color.green(another) * (1 - a) + 0.5).toInt()
        val blue =
            (Color.blue(color) * a + 0.5).toInt() + (Color.blue(another) * (1 - a) + 0.5).toInt()
        return 0xff shl 24 or (red shl 16) or (green shl 8) or blue
    }

    /**
     * 判断颜色是否为浅色
     *
     * @param color 颜色值
     * @return 是否为浅色
     */
    fun isLightColor(@ColorInt color: Int): Boolean {
        val darkness = 1 - (0.299 * Color.red(color) + 0.587 * Color.green(
            color
        ) + 0.114 * Color.blue(color)) / 255
        return darkness < 0.5
    }

    fun rgba2argb(rgba: Int): Int {
        return rgba shr 8 or (rgba shl 24)
    }
}